/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.master;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.SystemTray;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.synth.SynthLookAndFeel;

import com.melloware.jintellitype.JIntellitype;


public class SoftStarterGUI extends JFrame {

	private GUIStateController stateController;
	private BorderLayout layout;
	private JPanel basicView;
	private JPanel centreView;
	
    public SoftStarterGUI() {
        
        setUpWindow();
        setNimbusLookAndFeel();
//        setSynthlookAndFeel();
        checkIfOperationsAreSupported();
        checkWhichVersionOfWindows();
        
        build();
        buildGUI();
        
        setVisible(true);
    }
   
    private void setUpWindow() {
        this.setTitle("Firesoft");
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setUndecorated(true);
        getRootPane().setBackground(new Color(214,217,223));
//        getRootPane().setBorder(BorderFactory.createTitledBorder("Firesoft"));
        getRootPane().setBorder(BorderFactory.createLineBorder(Color.WHITE));

    }

    private void setNimbusLookAndFeel() {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                System.out.println(info.getName());
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void setSynthlookAndFeel() {
        try {
            SynthLookAndFeel laf = new SynthLookAndFeel();
            String userDir = System.getProperty("user.dir");
            laf.load(new URL("file:///" + userDir + "/resources/synth.xml"));
            UIManager.setLookAndFeel(laf);
        } catch(Exception e) {
            e.printStackTrace();
        }        
    }
    
	private void build() {
    	stateController = new GUIStateController(this);
    	basicView = new JPanel();
    	centreView = new JPanel();
//    	basicView.setBorder(new EmptyBorder(0, 0,0, 0));
//    	centreView.setBorder(new EmptyBorder(0, 0,0, 0));
    }
   
    private void buildGUI() {
    	
    	layout = new BorderLayout();
    	setLayout(layout);
    	
    	basicView.add(stateController.getCommandLineView());
    	basicView.add(stateController.getControllerButtonsView());
    	
    	add(basicView, BorderLayout.NORTH);
//    	add(centreView, BorderLayout.CENTER);
    	
    	pack();
	}
        
    private void checkIfOperationsAreSupported() {
        System.out.println(System.getProperty("os.arch") + " " + System.getProperty("os.version"));
        if(!Desktop.isDesktopSupported() || !SystemTray.isSupported()) {
            JOptionPane.showMessageDialog(this,
                    "Your operating environment doesn't support the operations of this program.",
                    "Sorry",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }
    
    private void checkWhichVersionOfWindows() {
        if(System.getProperty("os.arch").contains("64"))
            JIntellitype.setLibraryLocation("resources/JIntellitype64.dll");
        else
            JIntellitype.setLibraryLocation("resources/JIntellitype.dll");
    }
    
    public void showSettingsView(JPanel settingsView) {
        
        centreView.add(settingsView);
        add(centreView, BorderLayout.CENTER);
        
    }
    
    public void hideSettingsView() {
        
        centreView.removeAll();
        remove(centreView);
    }
    
    /**
     * Starts the program.
     * 
     * @param args
     */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
				new SoftStarterGUI();
		    }
		});
	}

}
